import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @Author: cxx
 * 数据库备份与还原
 * @Date: 2018/4/28 19:56
 */
public class DbOperate {

    /**
     * 备份数据库db
     *
     * @param root
     * @param pwd
     * @param dbName
     * @param backPath
     * @param backName
     */
    public static void dbBackUp(String root, String pwd, String dbName, String backPath, String backName) throws Exception {
        String pathSql = backPath + backName;
        File fileSql = new File(pathSql);
        //创建备份sql文件
        if (!fileSql.exists()) {
            fileSql.createNewFile();
        }
        //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
        StringBuffer sb = new StringBuffer();
        sb.append("mysqldump");
        sb.append(" -h120.25.152.50");
        sb.append(" -u" + root);
        sb.append(" -p" + pwd);
        sb.append(" " + dbName + " >");
        sb.append(pathSql);
        System.out.println("cmd命令为：" + sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始备份：" + dbName);//+sb.toString()
        Process process = runtime.exec("cmd /c " + sb.toString()); //"cmd /c ":   /c路径下是有mysqldump.exe 不然就要改成(“mysql的安装路径”/bin),或者把(“mysql的安装路径”/bin)下的mysqldump.sql复制到/c
        System.out.println("备份成功!");


    }

    /**
     * 恢复数据库
     *
     * @param root
     * @param pwd
     * @param dbName
     * @param filePath mysql -hlocalhost -uroot -p123456 db < /home/back.sql
     */
    public static void dbRestore(String root, String pwd, String dbName, String filePath) {
        StringBuilder sb = new StringBuilder();
        sb.append("mysql");
        sb.append(" -h120.25.152.50");
        sb.append(" -u" + root);
        sb.append(" -p" + pwd);
        sb.append(" " + dbName + " <");
        sb.append(filePath);
        System.out.println("cmd命令为：" + sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始还原数据");
        try {
            Process process = runtime.exec("cmd /c" + sb.toString());
            InputStream is = process.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is, "utf8"));
            String line = null;
            while ((line = bf.readLine()) != null) {
                System.out.println(line);
            }
            is.close();
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("还原成功！");
    }


    public static void main(String[] args) throws Exception {
        String st, st1, st2,st3;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数据库备份后的存放路径（例如 D:/A/ ）");
        st = scanner.next();
        System.out.println("输入数据库的名称（例如 1234.sql ）：");
        st1 = scanner.next();
        //String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+".sql";  //以时间命名数据库文件
        dbBackUp("root", "zqy939597A", "test", st, st1);
        System.out.println("是否恢复数据库？（y or n)");
        st2 = scanner.next();
        if (st2.equals("y")) {
            System.out.println("输入需要恢复的数据库的存放路径+数据库名称（例如 D:/A/1234.sql ）");
            st3 = scanner.next();
            dbRestore("root", "zqy939597A", "test", st3);
        }

    }
}